package escape;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName concurrent_study
 * @PackageName escape
 * @ClassName EventSource.java
 * @Description TODO
 * @Author yingyuwei
 * @Version 1.0.0
 * @CreateTime 2020年12月01日 20:57:00
 */
public class EventSource<T> {
    private final List<T> eventListeners ;
    public EventSource() {
        eventListeners = new ArrayList<T>() ;
    }

    public synchronized void registerListener(T eventListener) {  //数组持有传入对象的引用
        this.eventListeners.add(eventListener);
        this.notifyAll();
    }

    public synchronized List<T> retrieveListeners() throws InterruptedException {  //获取持有对象引用的数组
        List<T> dest = null;
        if(eventListeners.size() <= 0 ) {
            this.wait();
        }
        dest = new ArrayList<T>(eventListeners.size());
        dest.addAll(eventListeners);
        return dest;
    }

    public List<T> getEventListeners() {
        return eventListeners;
    }
}
